library pkg_pic_show;

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:photo_view/photo_view.dart';

class _PageModel {
  List<String> imageUrls;
  int index;
  bool isNetImage;
  bool isDark;

  _PageModel(this.imageUrls, this.index, this.isNetImage, this.isDark);
}

///图片预览
class ShowPic extends StatelessWidget {
  late final _PageModel _model;

  ShowPic({
    ///展示的图片的地址，可以使本地的绝对路径，也可以是网络url
    required List<String> imageUrls,

    ///多个图片的话，默认展示第几个
    required int index,

    ///是否是网络图片
    bool isNetImage = false,

    ///是否开启黑色背景
    bool isDark = false,
  }) {
    _model = _PageModel(imageUrls, index, isNetImage, isDark);
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Container(
          color: _model.isDark ? Color(0xff000000) : Color(0xffffffff),
          alignment: Alignment.center,
          child: SizedBox(
            width: MediaQuery.of(context).size.width,
            child: PageView(
              physics: BouncingScrollPhysics(),
              controller: PageController(initialPage: _model.index),
              children: _model.imageUrls
                  .map(
                    (e) => _model.isNetImage
                        ? PhotoView(imageProvider: NetworkImage(e))
                        : PhotoView(imageProvider: FileImage(File(e))),
                  )
                  .toList(),
            ),
          ),
        ),
        InkWell(
          splashColor: Colors.transparent,
          highlightColor: Colors.transparent,
          onTap: () => Navigator.pop(context),
          child: Container(
            width: 40,
            height: 40,
            margin: EdgeInsets.only(
              top: MediaQuery.of(context).padding.top + 10,
              left: 15,
            ),
            decoration: BoxDecoration(
              color: _model.isDark ? Color(0x33ffffff) : Color(0x22000000),
              borderRadius: BorderRadius.circular(90),
            ),
            child: Center(
              child: Icon(
                Icons.arrow_back,
                color: _model.isDark ? Color(0xffffffff) : Color(0xff000000),
                size: 20,
              ),
            ),
          ),
        ),
      ],
    );
  }
}
